import Vue from 'vue'
import VueCookies from 'vue-cookies'
import VueRouter from 'vue-router'
import Test from '../components/Test'
import MenuTop from '../components/MenuTop/'
import MenuLeft from '../components/MenuLeft'
import Section from '../components/Section'
import CrawlSiteList from '../components/CrawlSite/CrawlSiteList'
import CrawlSiteAdd from '../components/CrawlSite/CrawlSiteAdd'
import Login from '../components/Login'

Vue.use(VueRouter)
Vue.use(VueCookies)

const router = new VueRouter({
    routes: [
        {
            path: '/',
            name: '首页',
            components: {
                menutop: MenuTop,
                menuleft: MenuLeft,
                section: Section
            },
            children: [
                {
                    path: 'test',
                    name: '测试',
                    component: Test
                },
                {
                    path: 'crawlsitelist',
                    name: '站点列表',
                    component: CrawlSiteList
                },
                {
                    path: 'crawlsiteadd',
                    name: '抓取列表',
                    component: CrawlSiteAdd
                }
            ]
        },
        {
            path: '/login',
            name: '登录',
            component: Login
        }
    ]
})

router.beforeEach((to, from, next) => {
    let loginToken = router.app.$cookies.get('loginToken')
    if (!loginToken && to.path != '/login') {
        next({path: '/login'})
        return
    }
    next()

})

export default router